﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//You'll need Rectangle and Point from System.Drawing...
using System.Drawing;
using System.Windows.Forms;

namespace TheQuest
{
    class Game
    {
        ///<summary>
        ///The following two lines are OK as public properties if Enemy and Weapon are well encapsulated.
        ///In other words, just make sure the form can't do anything innappropriate with them.
        ///</summary>
        public List<Enemy> Enemies;
        public Weapon WeaponInRoom;
        
        //The game keeps a private Player object. 
        //The form willl only interact with this through methods on Game, rather than directly through the form.
        private Player player;
        public Point PlayerLocation { get { return player.Location; } }
        public int PlayerHitPoints { get { return player.HitPoints; } }
        public List<string> PlayerWeapons { get { return player.Weapons; } }

        private int level = 0;
        public int Level { get { return level; } }

        //The Rectangle object has Top, Bottom, Left and Right fields, and works perfectly for the overall game area.
        private Rectangle boundaries;
        public Rectangle Boundaries { get { return boundaries; } }

        public Game(Rectangle boundaries)
        {
            //Game starts out with a bounding box for the dungeon, and creates a new Player object in the dungeon.
            this.boundaries = boundaries;
            player = new Player(this, new Point(boundaries.Left + 10, boundaries.Top + 70));
        }

        public void Move(Direction direction, Random random)
        {
            //Movement is simple: move the player in the direction the form gives us, and move each enemy in a random direction.
            player.Move(direction);
            foreach (Enemy enemy in Enemies)
            {
                enemy.Move(random);
            }
        }

        public void Equip(string weaponName)
        {
            player.Equip(weaponName);
        }

        public bool CheckPlayerInventory(string weaponName)
        {
            return player.Weapons.Contains(weaponName);
        }

        public void HitPlayer(int maxDamage, Random random)
        {
            player.Hit(maxDamage, random);
        }

        public void IncreasePlayerHealth(int health, Random random)
        {
            player.IncreaseHealth(health, random);
        }

        //Attack is almost exactly like Move(), the player attacks, and the enemies all get a turn to move.
        public void Attack(Direction direction, Random random)
        {
            player.Attack(direction, random);
            foreach (Enemy enemy in Enemies)
		    enemy.Move(random);           
        }

        private Point GetRandomLocation(Random random)
        {
            //This is just a math trick to get a random location within the rectangle that represents the dungeon area.
            return new Point (boundaries.Left + random.Next(boundaries.Right / 10 - boundaries.Left / 10) * 10,
            boundaries.Top + random.Next(boundaries.Bottom / 10 - boundaries.Top /10) * 10);
        }

        public void NewLevel(Random random)
        {
            level++;
            switch(level)
            {
                case 1:
                    //Level 1, Enemy: Bat, Weapons: Sword.
                    Enemies = new List<Enemy>();
                    Enemies.Add(new Bat(this, GetRandomLocation(random)));
                    WeaponInRoom = new Sword(this, GetRandomLocation(random));
                    break;
                case 2:
                    //Level 2, Enemy: Ghost, Weapons: Blue Potion.
                    Enemies = new List<Enemy>();
                    Enemies.Add(new Ghost(this, GetRandomLocation(random)));
                    WeaponInRoom = new BluePotion(this, GetRandomLocation(random));
                    break;
                case 3:
                    //Level 3, Enemy: Ghoul, Weapons: Bow.
                    Enemies = new List<Enemy>();
                    Enemies.Add(new Ghoul(this, GetRandomLocation(random)));
                    WeaponInRoom = new Bow(this, GetRandomLocation(random));
                    break;
                case 4:
                    //Level 4, Enemy: Bat, Ghost, Weapons: Bow, if not picked up on 3; otherwise, blue potion, if not currently in the inventory.
                    Enemies = new List<Enemy>();
                    Enemies.Add(new Bat(this, GetRandomLocation(random)));
                    Enemies.Add(new Ghost(this, GetRandomLocation(random)));
                    if (!CheckPlayerInventory("Bow"))
                        WeaponInRoom = new Bow(this, GetRandomLocation(random));
                    else if (!CheckPlayerInventory("Blue Potion"))
                        WeaponInRoom = new BluePotion(this, GetRandomLocation(random));
                    break;
                case 5:
                    //Level 5, Enemy: Bat, Ghoul, Weapons: Red Potion.
                    Enemies = new List<Enemy>();
                    Enemies.Add(new Bat(this, GetRandomLocation(random)));
                    Enemies.Add(new Ghoul(this, GetRandomLocation(random)));
                    WeaponInRoom = new RedPotion(this, GetRandomLocation(random));
                    break;
                case 6:
                    //Level 6, Enemy: Ghost, Ghoul, Weapons: Mace.
                    Enemies = new List<Enemy>();
                    Enemies.Add(new Ghost(this, GetRandomLocation(random)));
                    Enemies.Add(new Ghoul(this, GetRandomLocation(random)));
                    WeaponInRoom = new Mace(this, GetRandomLocation(random));
                    break;
                case 7:
                    //Level 7, Enemy: Bat, Ghost, Ghoul Weapons: Mace, if not picked up on 6; otherwise, red potion, if not currently in the inventory.
                    Enemies = new List<Enemy>();
                    Enemies.Add(new Bat(this, GetRandomLocation(random)));
                    Enemies.Add(new Ghost(this, GetRandomLocation(random)));
                    Enemies.Add(new Ghoul(this, GetRandomLocation(random)));
                    if (!CheckPlayerInventory("Mace"))
                        WeaponInRoom = new Mace(this, GetRandomLocation(random));
                    else if (!CheckPlayerInventory("Mace"))
                    WeaponInRoom = new RedPotion(this, GetRandomLocation(random));
                    break;
                case 8:
                    //Level 8, Enemy: N/A, Weapons: N/A - end the game with Application.Exit()
                    System.Windows.Forms.MessageBox.Show("Nice! :)");
                    System.Windows.Forms.MessageBox.Show("GG, thanks for playing!");
                    Application.Exit();
                    break;
            }
        }
    }

    ///Move(Direction)
    ///{
    ///     Move the player's picturebox...player.Move(direction);
    ///     Let all the enemies move and
    ///     if they are in range
    ///     attack the player.
    ///     
    ///     This likely means a list of enemy objects will be required
    ///     use a foreach loop to... foreach (Enemy enemy in Enemies)
    ///     {
    ///     move each enemy... Enemy.Move(direction)
    ///     and determine if the player is within attack range...if (NearPlayer())
    ///     if they are then they hit the player...game.HitPlayer();
    ///}
    ///
}
